# -*- coding: utf-8 -*-
# coding=utf-8
from flask import Flask,render_template,abort
import time
from flask import request
import json
import os
# 获取配置文件
whiteListConfig = json.load(open(os.getcwd() + "/sysConfig/whiteList.json", 'r', encoding="utf-8"))
whiteIpList = whiteListConfig['whiteIpList']
whiteReqlist = whiteListConfig['whiteReqlist']

app = Flask(__name__)

@app.before_request
def ip_check():
    # 定义一个名单
    # whiteIpList = ['127.0.0.1', '127.0.0.2', '127.0.0.3']
    # 获取当前访问的URL
    path = request.remote_addr
    # 判断是否白名单中
    if path not in whiteIpList:
        print(path+'Ip不在名单里面')
        abort(404)
        return
    else:
        return

@app.before_request
def ip_check():
    # 定义一个名单
    # whiteReqlist = ['/mapserver/pbfServer/', '/user/user', '/user/image']
    # 获取当前访问的URL
    path = request.path
    # 放掉/根目录请求
    if path=="/":
        return
    # 判断是否白名单中
    whiteReqlistTrue=0
    for ii in whiteReqlist:
        if(path.rfind(ii) != -1):
            whiteReqlistTrue=1
            break
    if(whiteReqlistTrue==1):
        return
    else:
        print(path + '请求路径不在名单里面')
        abort(404)
        return

def tk_check(tk):
    try:
        aa=time.mktime(time.strptime(tk, "%Y %m %d"))
    except:
        return False
    else:
        if aa > time.time():
            return True
        else:
            return False
# 测试服务
@app.route('/index')
def getTest():
        return render_template("index.html")
        # abort(404)
        # response = make_response("地图资源中心访问正常。")
        # response.headers['Accept-Ranges'] = 'bytes'
        # response.headers['Access-Control-Allow-Origin'] = '*'
        # # response.headers['Content-Type'] = 'image/png'
        # return response

@app.route('/testError')
def testError():
    abort(404)

@app.errorhandler(404)
def not_fond(e):
    return render_template("exception/404.html")
    # return render_template("exception/404.html")

@app.errorhandler(500)
def in_Error(e):
    return render_template("index.html")
    # return render_template("exception/404.html")

'''
引入各模块
'''
from flaskController import tiffHandleController
